package com.yx.utils;

import com.yx.pojo.Result;
import java.io.Serializable;

public class ResultUtils<T> implements Serializable {

    private static final long serialVersionUID = -8904403178289000154L;
    private final Result<T> result;

    public ResultUtils(){
        result = new Result<>();
        result.setSuccess(true);
        result.setMessage("success");
        result.setErrorcode("0");
    }

    public Result<T> setData(T data){
        this.result.setData(data);
        this.result.setErrorcode("0");
        return this.result;
    }

    public Result<T> setData(T data, String msg){
        this.result.setData(data);
        this.result.setErrorcode("0");
        this.result.setMessage(msg);
        return this.result;
    }

    public Result<T> setSuccessMsg(String msg){
        this.result.setSuccess(true);
        this.result.setMessage(msg);
        this.result.setErrorcode("0");
        this.result.setData(null);
        return this.result;
    }

    public Result<T> setErrorMsg(String msg){
        this.result.setSuccess(false);
        this.result.setMessage(msg);
        this.result.setErrorcode("1");
        return this.result;
    }

    public Result<T> setErrorMsg(T data, String msg){
        this.result.setSuccess(false);
        this.result.setMessage(msg);
        this.result.setData(data);
        this.result.setErrorcode("1");
        return this.result;
    }

    public Result<T> setErrorMsg(String code, String msg){
        this.result.setSuccess(false);
        this.result.setMessage(msg);
        this.result.setErrorcode(code);
        return this.result;
    }

    public static <T> Result<T> success(T t){
        return new ResultUtils<T>().setData(t);
    }

    public static <T> Result<T> success(T t, String msg){
        return new ResultUtils<T>().setData(t, msg);
    }

    public static <T> Result<T> success(){
        return new ResultUtils<T>().result;
    }

    public static <T> Result<T> success(String msg){
        return new ResultUtils<T>().setSuccessMsg(msg);
    }

    public static <T> Result<T> fail(String msg){
        return new ResultUtils<T>().setErrorMsg(msg);
    }

    public static <T> Result<T> fail(String code, String msg){
        return new ResultUtils<T>().setErrorMsg(code, msg);
    }

    public static <T> Result<T> fail(T t,String msg){
        return new ResultUtils<T>().setErrorMsg(t,msg);
    }

    public static <T> Result<T> fail(T t){
        return new ResultUtils<T>().setErrorMsg(t,null);
    }
}
